/*
Java source file
Copyright: Umschalt.com 2012
*/
package com.david.documentation.web.view.pages;

import com.david.documentation.service.doku.DokuService;
import com.david.documentation.service.search.SearchService;
import com.david.documentation.web.BasePage;
import com.david.documentation.web.view.components.AutocompleteDataBehavior;
import com.david.documentation.web.view.dataProvider.CategoryDataProvider;
import com.david.documentation.web.view.dataProvider.SearchResultDataProvider;
import com.david.documentation.web.view.dataProvider.TagDataProvider;
import com.david.documentation.web.view.panels.SearchResultPanel;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.spring.injection.annot.SpringBean;

public class SearchResultPage extends BasePage{

	@SpringBean
	private SearchService searchService;

	@SpringBean
	private DokuService dokuService;

	public static final String SEARCH_PARAMETER = "search";
	public static final String TYPE_PARAMETER = "type";
	public static final String CATEGORY_TYPE = "Category";
	public static final String TAG_TYPE = "Tag";

	public SearchResultPage(){
		this(null);
	}
	public SearchResultPage(PageParameters parameters){
		super(parameters);
		
		String suche = "";

		if (!parameters.containsKey(SEARCH_PARAMETER)){
			add(new EmptyPanel("results"));
		} else if (parameters.containsKey(SEARCH_PARAMETER) && parameters.containsKey(TYPE_PARAMETER)){
			suche = parameters.get(SEARCH_PARAMETER).toString();
			if (parameters.get(TYPE_PARAMETER).toString().equals(AutocompleteDataBehavior.KATGEORY_VALUE)){
				add(new SearchResultPanel("results", new CategoryDataProvider(suche,dokuService), suche));
			} else if (parameters.get(TYPE_PARAMETER).toString().equals(AutocompleteDataBehavior.TAG_VALUE)){
				add(new SearchResultPanel("results", new TagDataProvider(suche,dokuService),suche));
			} else{
				add(new SearchResultPanel("results", new SearchResultDataProvider(suche,searchService), suche));
			}
		} else if (parameters.containsKey(SEARCH_PARAMETER) && !parameters.containsKey(TYPE_PARAMETER)){
			suche = parameters.get(SEARCH_PARAMETER).toString();
			add(new SearchResultPanel("results", new SearchResultDataProvider(suche,searchService), suche));
		}
		else{
			add(new EmptyPanel("results"));
		}
	}

}
